<?php
class General_Model extends Model
{ 
	function General_Model()
	{ 
		parent::Model();
	}
	  
	function getAdminAttributes( $keyname )
	{ 
		$this->db->select( 'value' );
		$this->db->where('key_name' , $keyname );
		$query = $this->db->get('admin_user_settings');
		return $query->row()->value; 
	}  
	
	function get_post_array( $not )
	{ 
		 $array = array(); 
		 $CI = &get_instance(); 
		 foreach($_POST as $key=>$value)
		 { 
			$match=false; 
			foreach($not as $vals)
			{ 
				if ($key==$vals) 
				{ 
				  $match=true; 	
				} 
			} 
			if ($match == false)
			{ 
				$array[$key] = $CI->input->post($key); 
			} 
		  } 
		return $array; 
	} 
	
	function get_posted_value( $not )
	 {	   
	   foreach($_POST as $key=>$value)
		 { 			 
			$match=false; 
			foreach($not as $vals)
			{ 
				if ($key==$vals) 
				{ 
				  $match=true; 	
				} 
			} 
			if ($match == false)
			 { 
				$this->session->set_flashdata($key,$value); 
			 } 	
			
		 } 
	 
	 }
	 
	 
	 
  function do_upload($image,$prev_image=NULL)
	{
		$config['upload_path'] = $this->config->item('banner_images');
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1024';
		$config['max_width']  = '145';
		$config['max_height']  = '245';
		
		$this->load->library('upload', $config);
	
		if ( ! $this->upload->do_upload($image))
		{
			//$this->session->set_flashdata('error_message', $this->upload->display_errors());
			$_POST['error_message'] = $this->upload->display_errors();
			return false;
		}	
		else
		{
			if($prev_image)
			{
				if(is_file($config['upload_path'].$prev_image))
				{
					@unlink($config['upload_path'].$prev_image);
				}
			}
			
		 	$data = array('upload_data' => $this->upload->data());
			$uploaded_image = $data['upload_data']['file_name'];
			return $uploaded_image;
		}
	}
	
	function get_user_name($id, $level)
	{
		$this->db->select('first_name, last_name');
		$this->db->where('id',$id);

		if($level == 'park')
		{
			$query = $this->db->get('tbl_park_admin_login_info');
		}
		else if($level == 'admin')
		{
			$query = $this->db->get('tbl_super_admin_login');
		}
		else if($level == 'player/coach')
		{
			$query = $this->db->get('tbl_team_members_info');		
		}
		$result = $query->row();
		return ucfirst($result->first_name)."&nbsp;".ucfirst($result->last_name);
	}	
	
	function get_messaging_user_info($id, $level)
	{
		if($level == 'park')
		{
			$this->db->select('a.park_name, a.city_address, b.state');
			$this->db->from('view_park_user_info as a');
			$this->db->join('tbl_states as b', 'a.state_id = b.state_id');
			$this->db->where('park_user_id', $id);
			$query = $this->db->get();
			$result = $query->row();
			return $result->park_name.', &nbsp;'.$result->state.', &nbsp;'.$result->city_address;
		}
		else if($level == 'player/coach')
		{
			$this->db->select('team_name, league_name');
			$this->db->where('member_id', $id);
			$query = $this->db->get('view_park_leauge_team_member');
			$result = $query->row();
			return $result->team_name.', &nbsp;'.$result->league_name;
		}
	
	}
	
	function get_user_avatar($id, $level)
	{
		if($level == 'player/coach')
		{
			$this->db->select('personal_image');
			$this->db->where('id',$id);
			$query = $this->db->get('tbl_team_members_info');		
			$result = $query->row();
			return $result->personal_image;																					
		}
		else if($level == 'admin')
			return 'super_admin.jpg';
		else if($level == 'park')
			return 'park_admin.jpg';
		else
			return false;
	}
	
	function validate_zipcode($zip_code)
	{
		$this->db->where('zip_code', $zip_code);
		$this->db->from('tbl_zipcode');
		if($this->db->count_all_results() == 1)
			return true;
		else
			return false;
	}
	
	function get_emails_to_send($limit=20)
	{
		$this->db->select('*');
		$this->db->order_by('id', 'asc');
		$query = $this->db->get('tbl_emails_to_send', $limit);
		return $query->result();
	}
	
	function get_email_reminders()
	{
		$sql = "SELECT * FROM tbl_reminders WHERE `time` BETWEEN CURTIME() AND ADDTIME(CURTIME(), '1:15:00') AND `date` = CURDATE()";
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	function get_finished_games()
	{
		$sql = "SELECT id, first_team_id, second_team_id FROM tbl_game_info WHERE game_date = CURDATE() AND notification = 0 AND CURTIME() >= ADDTIME(game_time, '3:00:00')";
		$query = $this->db->query($sql);
		return $query->result();
	}

	function get_sms_reminders()
	{
		$sql = "SELECT * FROM tbl_sms_reminders WHERE `time` BETWEEN CURTIME() AND ADDTIME(CURTIME(), '1:00:00') AND `date` = CURDATE()";
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	function delete_messages($user_id, $user_type)
	{
		$this->db->select('message_id');
		$this->db->where('sender_id = '.$user_id.' and sent_by = "'.$user_type.'"');
		$query = $this->db->get('tbl_general_recipient_inbox');
		$result = $query->result();
		
		foreach($result as $row)
		{
			$this->db->where('id', $row->message_id);
			$this->db->delete('tbl_general_inbox');
			$this->db->flush_cache();
		}
		
		$sql = "SELECT id, message_id AS msg_id, (SELECT COUNT(0) FROM tbl_general_recipient_inbox WHERE message_id = msg_id) AS total FROM tbl_general_recipient_inbox WHERE recipient_id = ".$user_id." AND sent_to = '".$user_type."'";
		
		$query = $this->db->query($sql);
		$result = $query->result();
		
		/*echo $this->db->last_query();*/
		//print_r($result);
		//echo count($result);
		foreach($result as $row)
		{
			if($row->total == 1)
			{
				$this->db->where('id', $row->msg_id);
				$this->db->delete('tbl_general_inbox');	
				//echo $this->db->last_query().'<br>';		
			}else
			{
				$this->db->where('id', $row->id);	
				$this->db->delete('tbl_general_recipient_inbox');			
				//echo $this->db->last_query().'<br>';
			}
			$this->db->flush_cache();
		}
		
	}
	
	function get_mobile_operator($operator_id)
	{
		$this->db->where('id', $operator_id);
		$query = $this->db->get('tbl_sms_to_email_gateways');
		return $query->row();
	}
	
	function get_admins()
	{
		$query = $this->db->get('tbl_super_admin_login');
		return $query->result();
	}
	
} 
?>